package com.client;

import com.message.resp.LoginResp;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * 处理登录的响应
 */
@Slf4j
public class LoginRespHandler extends SimpleChannelInboundHandler<LoginResp> {

    private final Thread guiThread;

    public LoginRespHandler(Thread guiThread) {
        this.guiThread = guiThread;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, LoginResp msg) throws Exception {
        if (msg.isLoginSuccess()) {
            log.debug("登录成功！");
        } else {
            log.debug("登录失败，即将退出系统");
            /*
                    关闭 channel 之后，将自动执行后续的释放资源的相关操作
             */
            ctx.channel().close();
            guiThread.interrupt();
        }
        ctx.fireChannelRead(msg);
    }
}
